using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;

public partial class COM_COM_ARTICULO_IMAGEN : PaginaBaseFormView 
{
    private FileUpload _fulRuta_Origen;
    private string _pathBorrar = string.Empty;
    private bool _guardarFichero = false;
    private string _idComArticuloImagen = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        odsfvcom_articulo_imagen.Updating += new ObjectDataSourceMethodEventHandler(odsfvcom_articulo_imagen_Updating);
        fvcom_articulo_imagen.ItemUpdated += new FormViewUpdatedEventHandler(fvcom_articulo_imagen_ItemUpdated);
        fvcom_articulo_imagen.DataBound += new EventHandler(fvcom_articulo_imagen_DataBound);
        BtnComArticulo.Click += new EventHandler(BtnComArticulo_Click);
    }

    void BtnComArticulo_Click(object sender, EventArgs e)
    {
        string id = ((TextBox)Fv.FindControl("Com_Articulo_Id")).Text;
        string codigo = ((TextBox)Fv.FindControl("Com_Articulo_Codigo")).Text;
        string nombre = ((TextBox)Fv.FindControl("Com_Articulo_Nombre")).Text;

        Response.Redirect("~/COM/COM_ARTICULO.aspx" +
            "?Com_Articulo_Id=" + id +
            "&Com_Articulo_Codigo=" + codigo +
            "&Com_Articulo_Nombre=" + nombre);
    }

    void fvcom_articulo_imagen_DataBound(object sender, EventArgs e)
    {
        if (_guardarFichero && Fv.CurrentMode == FormViewMode.ReadOnly)
        {
            string strServerPath = ((TextBox)Fv.FindControl("Ruta_Imagen")).Text;
            string extension = Path.GetExtension(Server.HtmlEncode(_fulRuta_Origen.FileName));
            string nombreFichero = _idComArticuloImagen + extension;
            strServerPath = strServerPath.Replace(nombreFichero, "");
            DirectoryInfo serverPath = new DirectoryInfo(Server.MapPath(strServerPath));
            string path = serverPath.FullName + nombreFichero;
            _fulRuta_Origen.SaveAs(path);
        }
    }

    void fvcom_articulo_imagen_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            BorrarArchivo(_pathBorrar);

            if (_fulRuta_Origen.HasFile)
            {
                _guardarFichero = true;
                _idComArticuloImagen = e.NewValues["Id"].ToString();
            }
        }
    }



    void BorrarArchivo(string path)
    {
        if (string.IsNullOrEmpty(path)) return;
        try
        {
            File.Delete(Server.MapPath(path));
        }
        catch
        { }
    }

    void odsfvcom_articulo_imagen_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        List<DictionaryEntry> parametros = (List<DictionaryEntry>)e.InputParameters["parametros"];
        _fulRuta_Origen = (FileUpload)Fv.FindControl("fulRuta_Origen");

        string origenImagen = null;
        string imagen = null;

        if (_fulRuta_Origen.HasFile)
        {
            origenImagen = _fulRuta_Origen.PostedFile.FileName;
            imagen = _fulRuta_Origen.FileName;
        }

            parametros[BuscarParametro(parametros, "Ruta_Origen")] =
                    new DictionaryEntry("Ruta_Origen", _fulRuta_Origen.PostedFile.FileName);

            parametros[BuscarParametro(parametros, "Nombre")] =
                new DictionaryEntry("Nombre", _fulRuta_Origen.FileName);

            string extensionFichero = Path.GetExtension(_fulRuta_Origen.FileName);
            parametros[BuscarParametro(parametros, "Extension")] =
                new DictionaryEntry("Extension", extensionFichero);
            
            parametros[BuscarParametro(parametros, "Nombre_Origen")] =
                new DictionaryEntry("Nombre_Origen", _fulRuta_Origen.FileName);

            _pathBorrar = ((TextBox)Fv.FindControl("Ruta_Imagen")).Text;
        
    }

    private int BuscarParametro(List<DictionaryEntry> parametros, string key)
    {
        for (int i = 0; i < parametros.Count; i++)
        {
            if (parametros[i].Key.ToString() == key)
                return i;
        }
        return -1;
    }

    protected override Type TipoObjeto
    {
        get { return typeof(Com_Articulo_Imagen); }
    }

    protected override FormView Fv
    {
        get { return fvcom_articulo_imagen; }
    }

    protected override ObjectDataSource OdsDv
    {
        get { return odsfvcom_articulo_imagen; }
    }

    protected override string Contenedor
    {
        get { return "COM_ARTICULO_IMAGEN"; }
    }

    protected override void AsignarParametrosDataSourceDeatailsView(string id)
    {
        OdsDv.SelectParameters["Com_Articulo_Id"].DefaultValue = id;
    }
    private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
}
